package com.nuc.bigdata.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nuc.bigdata.app.service.HomeViewPagerService;
import com.nuc.bigdata.domain.entity.HomeViewPager;
import com.nuc.bigdata.domain.entity.ViewPager;
import com.nuc.bigdata.domain.vo.HomeViewPagerVO;
import com.nuc.bigdata.feign.PictureServiceFeign;
import com.nuc.bigdata.infra.mapper.HomeViewPagerMapper;
import com.nuc.bigdata.infra.mapper.ViewPagerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author JackLuo
 * @date 2019/11/14
 */
@Service
public class HomeViewPagerServiceImpl implements HomeViewPagerService {
    private final ViewPagerMapper vpMapper;

    private final HomeViewPagerMapper hvpMapper;

    private final PictureServiceFeign pictureService;


    @Autowired
    HomeViewPagerServiceImpl(ViewPagerMapper vpMapper,HomeViewPagerMapper hvpMapper,
                             PictureServiceFeign pictureService) {
        this.vpMapper = vpMapper;
        this.hvpMapper = hvpMapper;
        this.pictureService = pictureService;
    }

    /**
     * 获取所有的首页轮播图
     * @return 包含展示给前端的轮播图VO的ArrayList
     */
    @Override
    public ArrayList<HomeViewPagerVO> getAll() {
        ArrayList<HomeViewPagerVO> homeViewPagerVOArrayList = new ArrayList<>();

        List<HomeViewPager> homeViewPagers = hvpMapper.selectList(null);
        for (HomeViewPager hvp:homeViewPagers) {
            HomeViewPagerVO homeViewPagerVO = new HomeViewPagerVO();
            // 拿到hvp的id
            String vpId = hvp.getVpId();
            // 构造hvp中vpId和vp中vpId相等的条件
            QueryWrapper<ViewPager> viewPagerQw = new QueryWrapper<>();
            viewPagerQw.eq("vp_id",vpId ).orderByAsc("id");
            // 需要展示的vp
            ViewPager viewPager = vpMapper.selectOne(viewPagerQw);
            homeViewPagerVO.setId(viewPager.getVpId());
            // 获取图片对应的地址
            String pictureUrl = pictureService.getPictureUrl(viewPager.getPictureId());
            homeViewPagerVO.setImg("http://"+pictureUrl);
            homeViewPagerVO.setUrl(viewPager.getUrl());
            homeViewPagerVOArrayList.add(homeViewPagerVO);
        }
        return homeViewPagerVOArrayList;
    }
}
